package com.ksyun.ks3.services.handler;

import com.ksyun.ks3.exception.Ks3ClientException;
import com.ksyun.ks3.exception.Ks3ServiceException;
import com.ksyun.ks3.model.result.GetBucketReplicationResult;
import com.ksyun.ks3.model.ReplicationRule;
import com.ksyun.ks3.services.request.GetBucketReplicationRequest;
import com.ksyun.ks3.services.request.Ks3HttpRequest;
import com.ksyun.ks3.util.ResponseUtils;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Headers;

public abstract class GetBucketReplicationResponseHandler extends Ks3HttpResponseHandler {

    public abstract void onFailure(GetBucketReplicationRequest request, Ks3ClientException clientException, Ks3ServiceException serviceException);

    public abstract void onSuccess(GetBucketReplicationRequest request, GetBucketReplicationResult result);

    @Override
    public final void onSuccess(Ks3HttpRequest request, int statusCode, Headers responseHeaders, byte[] response) {
        GetBucketReplicationResult result = new GetBucketReplicationResult();
        result.setStatusCode(statusCode);
        result.setHeaders(ResponseUtils.parseResponseHeaders(responseHeaders));
        result.setReplicationRule(parseXml(response));
        this.onSuccess((GetBucketReplicationRequest) request, result);
    }

    @Override
    public final void onFailure(Ks3HttpRequest request, Ks3ClientException clientException, Ks3ServiceException serviceException) {
        this.onFailure((GetBucketReplicationRequest) request, clientException, serviceException);
    }

    @Override
    public final void onProgress(long bytesWritten, long totalSize) {
    }

    @Override
    public final void onStart() {
    }

    @Override
    public final void onFinish() {
    }

    @Override
    public final void onCancel() {
    }

    private ReplicationRule parseXml(byte[] response) {
        XmlPullParserFactory factory;
        ReplicationRule replicationRule = null;
        List<String> prefixList = null;
        try {
            factory = XmlPullParserFactory.newInstance();
            XmlPullParser parse = factory.newPullParser();
            parse.setInput(new ByteArrayInputStream(response), "UTF-8");
            int eventType = parse.getEventType();
            while (XmlPullParser.END_DOCUMENT != eventType) {
                String nodeName = parse.getName();
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        prefixList = new ArrayList<>();
                        break;
                    case XmlPullParser.END_DOCUMENT:
                        break;
                    case XmlPullParser.START_TAG:
                        if ("Replication".equalsIgnoreCase(nodeName) || "ns2:Replication".equalsIgnoreCase(nodeName)) {
                            replicationRule = new ReplicationRule();
                        }
                        if ("prefix".equalsIgnoreCase(nodeName)) {
                            prefixList.add(parse.nextText());
                            replicationRule.setPrefixList(prefixList);
                        }
                        if ("DeleteMarkerStatus".equalsIgnoreCase(nodeName)) {
                            if (parse.nextText().contentEquals("Enabled")) {
                                replicationRule.setDeleteMarkerStatus(true);
                            } else {
                                replicationRule.setDeleteMarkerStatus(false);
                            }
                        }
                        if ("targetBucket".equalsIgnoreCase(nodeName)) {
                            replicationRule.setTargetBucket(parse.nextText());
                        }
                        if ("region".equalsIgnoreCase(nodeName)) {
                            replicationRule.setRegion(parse.nextText());
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if (nodeName.equalsIgnoreCase("Replication")) {

                        }
                        break;
                    case XmlPullParser.TEXT:

                        break;
                    default:
                        break;
                }
                eventType = parse.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return replicationRule;
    }

}

